package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.adapter.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity111 extends AppCompatActivity implements ViewPager.OnPageChangeListener{
    private ViewPager viewPager;
    private ViewPagerAdapter myAdapter;
    private List<View> views;
    private ImageView[] dots;
    private int[] ids={R.id.iv1,R.id.iv2,R.id.iv3};
    private Button btnWelcome;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide111);
        init();
        initDots();
    }
    /**
     * 自定义初始化方法
     * 加载所要切换的view
     */
    private void init() {
        //使用layoutInflater来进行视图加载
        LayoutInflater inflater = LayoutInflater.from(this);
        //对集合进行实例化操作
        views = new ArrayList<View>();
        //layoutInflater的使用，root传入null
        views.add(inflater.inflate(R.layout.layout1, null));
        views.add(inflater.inflate(R.layout.layout2, null));
        views.add(inflater.inflate(R.layout.layout3, null));
        //实例化适配器
        myAdapter = new ViewPagerAdapter(views, this);
        //加载viewPager对象
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        //ViewPager绑定适配器
        viewPager.setAdapter(myAdapter);

        /*//查找id
        btnWelcome= (Button) views.get(2).findViewById(R.id.btnWelcome);
        btnWelcome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(GuideActivity111.this,MainActivity.class));
                finish();//结束掉不需要的界面
            }
        });*/
        //设置页面回调，设置事件监听器
        viewPager.addOnPageChangeListener(this);
    }



    public  void EnterLogin(View view)
    {
        Intent intent=new Intent(GuideActivity111.this,LoginActivity.class);
        startActivity(intent);
    }

    /**
     * 导航点功能实现
     */
    private void initDots(){
        dots=new ImageView[views.size()];
        for (int i=0;i<views.size();i++){
            dots[i]= (ImageView) findViewById(ids[i]);
        }

    }

    /**
     *当页面滑动时调用
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }


    /**
     *当新的页面被选择时调用
     * 当前的页面被选中，将当前页面的图片换成亮的
     * @param position
     */
    @Override
    public void onPageSelected(int position) {

        for (int i=0;i<ids.length;i++){
            if (i==position){
                dots[i].setImageResource(R.drawable.button_red_pressed);
            }
            else
                dots[i].setImageResource(R.drawable.button_red_normal);
        }
    }

    /**
     * 滑动状态改变时进行调用
     * @param state
     */
    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
